import type { ProColumns } from '@ant-design/pro-components';
import type { FormInstance } from 'antd';

export interface SearchFieldConfig {
  name: string;
  label: string;
  type: 'input' | 'select' | 'dateRange' | 'date' | 'number' | 'textarea';
  placeholder?: string;
  options?: Array<{ label: string; value: any }>;
  rules?: any[];
  props?: Record<string, any>;
  fieldProps?: Record<string, any>;
  required?: boolean; // 是否必填
  // 动态显示控制
  visible?: boolean | ((values: Record<string, any>) => boolean);
  // 动态依赖字段，当这些字段变化时重新计算可见性
  dependencies?: string[];
  // 动态选项，可以根据其他字段值来动态生成选项
  dynamicOptions?: (values: Record<string, any>) => Array<{ label: string; value: any }>;
}

export interface FormFieldConfig {
  name: string;
  label: string;
  type: 'input' | 'select' | 'dateRange' | 'date' | 'number' | 'textarea' | 'upload' | 'switch' | 'editTable';
  placeholder?: string;
  options?: Array<{ label: string; value: any }>;
  rules?: any[];
  props?: Record<string, any>;
  fieldProps?: Record<string, any>;
  span?: number;
  // 标签后缀，支持在label后面添加复制按钮等功能
  labelSuffix?: React.ReactNode | ((value: any) => React.ReactNode);
  // 可编辑表格配置
  editTableConfig?: {
    columns: Array<{
      title: string;
      dataIndex: string;
      type?: 'input' | 'select' | 'number' | 'date';
      options?: Array<{ label: string; value: any }>;
      width?: number;
      required?: boolean;
    }>;
    rowKey?: string;
    maxRows?: number;
    minRows?: number;
    addButtonText?: string;
    deleteButtonText?: string;
  };
  // 动态显示控制
  visible?: boolean | ((values: Record<string, any>) => boolean);
  // 动态依赖字段，当这些字段变化时重新计算可见性和其他属性
  dependencies?: string[];
  // 动态选项，可以根据其他字段值来动态生成选项
  dynamicOptions?: (values: Record<string, any>) => Array<{ label: string; value: any }>;
  // 动态必填控制，优先级高于 rules 中的 required
  required?: boolean | ((values: Record<string, any>) => boolean);
}

// 表单分组配置
export interface FormSection {
  title: string;
  fields: FormFieldConfig[];
  showDivider?: boolean; // 是否显示分割线，默认为 true
}

// CRUD操作配置接口
export interface CrudConfig {
  // API端点配置
  endpoints: {
    list?: string; // 查询列表
    create?: string; // 新增
    update?: string; // 更新
    delete?: string; // 删除
    detail?: string; // 详情
  };

  // 请求方法配置
  methods?: {
    list?: 'GET' | 'POST';
    create?: 'POST' | 'PUT';
    update?: 'POST' | 'PUT' | 'PATCH';
    delete?: 'POST' | 'DELETE';
    detail?: 'GET' | 'POST';
  };

  // 参数转换函数
  transformRequest?: {
    list?: (params: any) => any;
    create?: (data: any) => any;
    update?: (data: any) => any;
    delete?: (id: any) => any;
  };

  // 响应转换函数
  transformResponse?: {
    list?: (response: any) => { data: any[]; total?: number };
    create?: (response: any) => any;
    update?: (response: any) => any;
    delete?: (response: any) => any;
    detail?: (response: any) => any;
  };

  // 回调函数
  callbacks?: {
    beforeCreate?: (data: any) => Promise<any> | any;
    afterCreate?: (response: any, data: any) => Promise<void> | void;
    beforeUpdate?: (data: any) => Promise<any> | any;
    afterUpdate?: (response: any, data: any) => Promise<void> | void;
    beforeDelete?: (id: any) => Promise<boolean> | boolean;
    afterDelete?: (response: any, id: any) => Promise<void> | void;
    onError?: (error: any, operation: string) => void;
  };
}

// 操作按钮配置接口（从 utils 导入）
export interface ActionButtonConfig {
  key: string;
  label?: string;
  icon: React.ReactNode;
  danger?: boolean;
  type?: 'primary' | 'default' | 'link';
  className?: string;
  permission?: string;
}

// 搜索配置
export interface SearchConfig {
  fields?: SearchFieldConfig[];
  initialValues?: Record<string, any>;
  onSearch?: (values: Record<string, any>) => void;
  onReset?: () => void;
}

// 表格配置
export interface TableConfig<T = any> {
  columns: ProColumns<T>[];
  rowKey?: string | ((record: T) => string);
  props?: Record<string, any>;
}

// 表单配置
export interface FormConfig {
  fields?: FormFieldConfig[]; // 兼容旧的使用方式
  sections?: FormSection[]; // 新的分组方式
  initialValues?: Record<string, any>;
  onSubmit?: (values: Record<string, any>) => Promise<void>;
  onCancel?: () => void;
}

// 操作配置
export interface ActionConfig<T = any> {
  permissions?: {
    create?: boolean;
    update?: boolean;
    delete?: boolean;
    view?: boolean;
  };
  customActions?: ActionButtonConfig[];
  // onAction 返回值说明：
  // - void: 完全自定义处理（自定义模式，仅对自定义操作生效）
  // - T: 处理后的数据传给表单（非自定义模式）
  // - false: 取消操作
  // 注意：detail、edit 和 copy 操作总是走默认逻辑（打开抽屉表单），即使在自定义模式下
  //   - detail: 以查看模式打开表单（只读）
  //   - edit: 以编辑模式打开表单
  //   - copy: 以新增模式打开表单，但预填充当前记录数据（不包含 id）
  onAction?: (actionType: string, record: T) => void | T | false;
}

// 批量操作配置
export interface BatchActionConfig<T = any> {
  enabled?: boolean;
  actions?: Array<{
    key: string;
    label: string;
    action: (selectedRows: T[]) => Promise<void>;
    danger?: boolean;
  }>;
  renderCustom?: (selectedRowKeys: React.Key[], selectedRows: T[]) => React.ReactNode;
}

// 页面配置
export interface PageConfig {
  title?: string;
  breadcrumb?: {
    items?: Array<{
      title: React.ReactNode;
      href?: string;
    }>;
  };
  isShowBreadcrumb?: boolean;
}

// 页面模板配置接口
export interface PageTemplateProps<T = any> {
  pageConfig?: PageConfig;
  searchConfig?: SearchConfig;
  tableConfig: TableConfig<T>;
  formConfig?: FormConfig;
  actionConfig?: ActionConfig<T>;
  batchActionConfig?: BatchActionConfig<T>;
  crudConfig?: CrudConfig;
}

export interface SearchFormProps {
  fields: SearchFieldConfig[];
  initialValues?: Record<string, any>;
  onSearch: (values: Record<string, any>) => void;
  onReset: () => void;
}

export interface FormDrawerProps {
  visible: boolean;
  title: string;
  fields: FormFieldConfig[];
  initialValues?: Record<string, any>;
  onSubmit: (values: Record<string, any>) => Promise<void>;
  onCancel: () => void;
  formInstance?: FormInstance;
  width?: number;
}
export interface CommonTableProps<T = any> {
  columns: ProColumns<T>[];
  dataSource?: T[];
  loading?: boolean;
  pagination?: any;
  rowKey?: string | ((record: T) => string);
  tableProps?: Record<string, any>;
  showAddButton?: boolean;
  addButtonText?: string;
  onAdd?: () => void;
  extraButtons?: React.ReactNode;
  title?: string;
  bordered?: boolean;
}
